package com.study.system.log.controller;

import com.alibaba.fastjson.JSON;
import com.study.system.BaseServlet;
import com.study.system.log.domain.Log;
import com.study.system.log.domain.UserLog;
import com.study.system.log.service.Impl.LoginServiceImpl;
import com.study.system.user.domain.User;
import com.study.system.user.service.UserService;
import com.study.system.user.service.impl.UserServiceImpl;
import com.study.system.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @Author 669
 * @CreateDate 2022/4/20 11:56
 * @Version 1.0
 */
@WebServlet("/log/*")
public class LogController extends BaseServlet {
    public void addLog(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //获取登录信息
            // 获取请求体数据
            BufferedReader br = req.getReader();
            String params = br.readLine();
            // 将JSON字符串转为Java对象
            Log log = JSON.parseObject(params, Log.class);
            Log log1 = new Log();
            log1.setDate(DataUtils.getDate());
            log1.setEid(log.getId());
            log1.setStatus(log.getStatus());

            //添加请求
            LoginServiceImpl service = new LoginServiceImpl();
            service.addLog(log1);

            //返回响应
            resp.getWriter().write("true");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delLog(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            System.out.println(id);
            //添加请求
            LoginServiceImpl service = new LoginServiceImpl();
            int i = service.delById(Integer.parseInt(id));
            System.out.println(i);
            if (i == 1) {
                req.getRequestDispatcher("/jsp/admin/admin_log.jsp").forward(req, resp);
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
